package com.example.administrator.myproject.home

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.PopupWindow
import android.widget.TextView
import android.widget.Toast
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.SaveListener
import com.example.administrator.myproject.App
import com.example.administrator.myproject.R
import com.example.administrator.myproject.bean.BobmBean
import com.example.administrator.myproject.bean.CartModel
import com.example.administrator.myproject.bean.Collect
import com.example.administrator.myproject.utils.SPUtils
import kotlinx.android.synthetic.main.cart_view.*
import java.util.*
import kotlin.concurrent.fixedRateTimer

class Cart_c : AppCompatActivity() {
    private var popWnd: PopupWindow? = null
    private var mingcheng: EditText? = null
    private var recyclerView: RecyclerView? = null
    private var textview1: TextView?= null
    private var textview2: TextView?= null
    private var adapter: HomeAdapter? = null
    private var cartadapter: CartAdapter? = null
    private var timer: Timer? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.cart_view)
        mingcheng = findViewById(R.id.name_et)

        recyclerView = findViewById(R.id.cart_rv)
        recyclerView!!.layoutManager = LinearLayoutManager(this)
        //添加商品数据
        val list22: MutableList<CartModel> = ArrayList()
        val bmobQuery = BmobQuery<BobmBean>()

        var temp: Int? = null
        bmobQuery.findObjects(object : FindListener<BobmBean>() {
            override fun done(list: List<BobmBean>, e: BmobException?) {
                if (e == null) {
                    temp = list.size
                    for (i in 0..temp!! - 1) {
                        val cartModel = CartModel()
                        cartModel.setName(list[i].name)
                        cartModel.setPrice(list[i].money)
                        list22.add(cartModel)
                    }
//                    adapter!!.setData(list)
                } else {
                    Toast.makeText(this@Cart_c, "查询失败", Toast.LENGTH_SHORT).show()
                }
            }
        })

        Handler().postDelayed({
            // TODO Auto-generated method stub
            println(temp)
            adapter = HomeAdapter(this, list22)
            recyclerView!!.adapter = adapter
            bmobQuery.findObjects(object : FindListener<BobmBean>() {
                override fun done(list: List<BobmBean>, e: BmobException?) {
                    if (e == null) {
                    adapter!!.setData(list)
                    } else {
                        Toast.makeText(this@Cart_c, "查询失败", Toast.LENGTH_SHORT).show()
                    }
                }
            })
        }, 1000)

        buy.setOnClickListener{
            var tmp: Int? = 0
            for (i in 0..temp!! - 1) {
                if(list22[i].getCount() != 0) {
                    val cartModel = CartModel()
                    cartModel.setName(list22[i].getName())
                    cartModel.setCount(list22[i].getCount())
                    cartModel.setPrice(list22[i].getPrice())
                    println(list22[i].getName())
                    println(tmp)
                    tmp = tmp!! + list22[i].getCount()!! * list22[i].getPrice()!!
                }
            }
            var application: App = this!!.application as App
            var mymoney = application.getMoney()
            if(mymoney!! >= tmp!!) {
                val collect = Collect()
                collect.name = "知名缘泡椒牛肉米线"
                collect.money = tmp.toString()
                collect.state = "1"
                collect.coum = "1"
                collect.save(object : SaveListener<String>() {
                    override fun done(s: String, e: BmobException?) {
                        if (e == null) {
                            application.setMoney(mymoney - tmp!!)
                            Toast.makeText(this@Cart_c, "支付成功", Toast.LENGTH_SHORT).show()
                            finish()
                        } else {
                            Toast.makeText(this@Cart_c, "购买失败", Toast.LENGTH_SHORT).show()
                        }
                    }
                })
            } else{
                Toast.makeText(this, "余额不足", Toast.LENGTH_SHORT).show()
            }

        }
        db.setOnClickListener {
            val contentView = LayoutInflater.from(this).inflate(R.layout.dibu, null)
            val quxiao = contentView.findViewById<TextView>(R.id.qx)

            quxiao.setOnClickListener{
                popWnd!!.dismiss()
            }
            textview1 = contentView.findViewById(R.id.txt_money) as TextView
            recyclerView = contentView.findViewById(R.id.cart_rv2)
            recyclerView!!.layoutManager = LinearLayoutManager(this)
            cartadapter = CartAdapter(this)
            recyclerView!!.adapter = cartadapter
            println(temp)
            val list33: MutableList<CartModel> = ArrayList()
            var tmp: Int = 0
            for (i in 0..temp!! - 1) {
                if(list22[i].getCount() != 0) {
                    val cartModel = CartModel()
                    cartModel.setName(list22[i].getName())
                    cartModel.setCount(list22[i].getCount())
                    cartModel.setPrice(list22[i].getPrice())
                    println(list22[i].getName())
                    println(tmp)
                    tmp = tmp!! + list22[i].getCount()!! * list22[i].getPrice()!!
                    list33.add(cartModel)
                }
            }
            textview1!!.setText(tmp.toString())



            cartadapter!!.setData(list33)

            popWnd = PopupWindow(this)
            popWnd!!.contentView = contentView
            popWnd!!.width = ViewGroup.LayoutParams.MATCH_PARENT
            popWnd!!.height = ViewGroup.LayoutParams.MATCH_PARENT
            popWnd!!.setAnimationStyle(R.style.anim_menu_bottombar)
            popWnd!!.showAtLocation(mingcheng, Gravity.BOTTOM, 0, 0)

        }

    }
}